package com.cpecc.controller.system;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.manager.system.WuZiLBManager;
import com.cpecc.model.system.WuZiLB;

@Controller
@RequestMapping(value = "/system")
public class WuZiLBController {
	
	@Autowired
	private WuZiLBManager wuZiLBManager;
	
	@RequestMapping(value = "/wuziLB")
	public ModelAndView wuziLB(){
		ModelAndView mav=new ModelAndView();
		StringBuilder bs=new StringBuilder();
		bs.append("[");
		List<WuZiLB> list=wuZiLBManager.getAllWuZiLB();
		for(WuZiLB wuZiLB:list){
			bs.append("{id:\""+wuZiLB.getId()+"\"");
			if(wuZiLB.getParent()!=null)
				bs.append(",pId:\""+wuZiLB.getParent().getId()+"\"");
			else
				bs.append(",pId:\"0\"");
			bs.append(",name:\""+wuZiLB.getLeibieMC()+"\"},");
		}
		bs.delete(bs.length()-1, bs.length());
		bs.append("]");
		String result=bs.toString();
		mav.addObject("result", result);
		mav.setViewName("system/wuZiLB");
		return mav;
	}
	
	@RequestMapping(value = "/getWuZiLBDetail")
	public ModelAndView getWuZiLBDetail(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZiLBDetail", wuZiLBManager.get(id));
		mav.setViewName("system/wuZiLBDetailAjax");
		return mav;
	}
	
	@RequestMapping(value = "/tianJiaXiaJiLB")
	public ModelAndView tianJiaXiaJiLB(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("pId", id);
		mav.setViewName("system/editWuZiLB");
		return mav;
	}
	
	@RequestMapping(value = "/wuZiLBSave")
	public String wuZiLBSave(String pId,WuZiLB wuZiLB){		
		if(!wuZiLB.getId().equals("")&&wuZiLB.getId()!=null){					
			wuZiLBManager.cover(wuZiLB);
		}else{
			wuZiLB.setCengJi(wuZiLBManager.get(pId).getCengJi()+1);
			wuZiLB.setParent(wuZiLBManager.get(pId));			
			wuZiLBManager.save(wuZiLB);
		}
		return "redirect:/system/wuziLB.do";
	}
	
	@RequestMapping(value = "/xiuGaiLBXX")
	public ModelAndView xiuGaiLBXX(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("id", id);		
		mav.addObject("wuZiLBDetail", wuZiLBManager.get(id));
		mav.setViewName("system/editWuZiLB");
		return mav;
	}
	
	@RequestMapping(value = "/shanChuLB")
	@ResponseBody
	public void shanChuLB(String id,HttpServletResponse response){
		String result="{\"result\":\"0\"}";		
		WuZiLB wuZiLB=wuZiLBManager.get(id);
		if(wuZiLB.getChildren().size()>0)		
			result="{\"result\":\"1\"}";
		else
			wuZiLBManager.delete(wuZiLB);
		PrintWriter out = null;
		response.setContentType("application/json");		
		try {
			out = response.getWriter();
			out.write(result);
		} catch (Exception e) {
		}
	}
	
	@RequestMapping(value = "/getAllWuZiLB2")
	public ModelAndView getAllWuZiLB2(String id){		
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZiLBList", wuZiLBManager.getAllWuZiLB2(id));
		mav.setViewName("wuzi/leiBieSelectAjax");
		return mav;
	}
}
